package pub.dsb.spring.bean.componentscan;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2020/12/11 17:38
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2020/12/11 17:38
 * @modify reason: {方法名}:{原因}
 * ...
 * @since V1.0.0
 */
@Slf4j
public class HelloBeanFactoryPostProcess extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        log.info("[BeanFactoryPostProcessor] postProcessBeanFactory");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("[InstantiationAwareBeanPostProcessorAdapter] postProcessBeforeInitialization " + beanName);
        return super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        log.info("[InstantiationAwareBeanPostProcessorAdapter] postProcessProperties " + beanName);
        if (bean instanceof HelloBean && "secondHelloBean".equals(beanName)) {
            HelloBean instance = (HelloBean) bean;
            log.info("OLD NAME,{}", instance.getName());
            instance.setName("AAAA");
        }
        return super.postProcessProperties(pvs, bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("[InstantiationAwareBeanPostProcessorAdapter] postProcessAfterInitialization " + beanName);
        return super.postProcessAfterInitialization(bean, beanName);
    }
}
